package cn.qn.controller;

import cn.qn.base.PageVO;
import cn.qn.common.util.UserSubjectUtil;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.sys.SysMessageDTO;
import cn.qn.dto.sys.SysMessageRequestDTO;
import cn.qn.moudle.sys.SysMessage;
import cn.qn.moudle.user.AppUser;
import cn.qn.service.sys.SysMessageService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @author Jeet
 * @description 系统消息
 * @date 2019/7/26 17:11
 */
@RestController
@RequestMapping("/sys/message")
public class SysMessageController {

    @Reference(check = false,timeout = 30000)
    private SysMessageService sysMessageService;

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public ResponseDTO page(SysMessageRequestDTO request,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        request.setUserId(appUser.getUserId());
        PageVO<SysMessageDTO> pageVO = sysMessageService.page(request);
        return ResponseDTO.success(pageVO);
    }

    @RequestMapping(value = "/new",method = RequestMethod.GET)
    public ResponseDTO newestMsg(@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        SysMessageDTO dto = sysMessageService.findNewMessage(appUser.getUserId());
        return ResponseDTO.success(dto);
    }

    @RequestMapping(value = "/unread/count",method = RequestMethod.GET)
    public ResponseDTO countUnReadMsg(@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        Integer count = sysMessageService.countByUserIdUnReadMsg(appUser.getUserId());
        return ResponseDTO.success(count);
    }
}
